package com.dalin.framework.common.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.dalin.framework.common.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dalin.framework.common.PropertyConst;

/*****************************************************************************
 * @content: 文本文件操作常用方法汇总                                                                                                                                  *
 * @author : 那建林                                                                                                                                                                  *
 * @crtDate: 2010-03-17                                                     *
 ****************************************************************************/
public final class TextUtils {
	private TextUtils() {} // 不允许构造
	private static final Log log = LogFactory.getLog(TextUtils.class);


	/*************************************************************************
	 * @content: 把整个文件都读写到一个字符串中
	 * @param  : srcFile 待读取的文本文件
	 * @param  : encoding 文件的编码格式 
	 * @return : String 无换行符，无空行，每行字符串两边的空格去掉了
	 * @author : 那建林
	 * @crtDate: 2010-03-16
	 * @modDate: 2010-03-16
	 */
	public static String readNoLine(File srcFile, String encoding) throws Exception {
		if (!srcFile.exists()) {
			 throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
		}
		StringBuilder sb = new StringBuilder();
		FileReader fr = new FileReader(srcFile);
		BufferedReader br = null;
		if (StringUtils.isBlank(encoding)) {
			br = new BufferedReader(new FileReader(srcFile));
		} else {
			br = new BufferedReader(new InputStreamReader(
					new FileInputStream(srcFile), encoding));
		}
		String tempStr;
		while ((tempStr = br.readLine()) != null) {
			sb.append(tempStr.trim());
		}
		br.close();
		fr.close();
		return sb.toString();
	}
	
	
	/*************************************************************************
	 * @content: 把整个文本文件按行读取出来，放到list中,保持原样，空格空行全保留
	 * @param  : srcFile 待读取的文本文件
	 * @param  : encoding 文件的编码格式 
	 * @return : List<String> 一条List记录相当于一行文本数据
	 * @author : 那建林
	 * @crtDate: 2010-03-16
	 * @modDate: 2010-03-16
	 */
	public static List<String> readByList(
			File srcFile, String encoding) throws Exception {
		if (!srcFile.exists()) {
			 throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
		}
		List<String> rtnList = new ArrayList<String>();
		BufferedReader br = null;
		if (StringUtils.isBlank(encoding)) {
			br = new BufferedReader(new FileReader(srcFile));
		} else {
			br = new BufferedReader(new InputStreamReader(
					new FileInputStream(srcFile), encoding));
		}
		String tempStr;
		while ((tempStr = br.readLine()) != null) {
			rtnList.add(tempStr);
		}
		br.close();
		return rtnList;
	}
	
	
	/*************************************************************************
	 * @content: 将字符串写入到文本文件
	 * @param  : str 要保存的字符串
	 * @param  : destFile 要保存的文件
	 * @author : 那建林
	 * @crtDate: 2010-03-16
	 * @modDate: 2010-03-16
	 */
	public static void overwriteToText(String str, File destFile) throws Exception {
		destFile.getParentFile().mkdirs();
		FileWriter fw = new FileWriter(destFile);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(str);
		bw.write(PropertyConst.LINE_SEPARATOR);
		bw.flush();
		bw.close();
		fw.close();
	}
	public static void overwriteToText(String str, File destFile, String encoding) throws Exception {
		destFile.getParentFile().mkdirs();
		FileOutputStream fout = new FileOutputStream(destFile); 
		OutputStreamWriter writer = new OutputStreamWriter(fout, encoding); 
		writer.write(str); 
		writer.flush(); 
		writer.close(); 
		fout.close(); 
	}
	
	
	/*************************************************************************
	 * @content: 追加字符串到文本文件尾部
	 * @param  : str 要保存的字符串
	 * @param  : destFile 要保存的文件
	 * @author : 那建林
	 * @crtDate: 2010-03-16
	 * @modDate: 2010-03-16
	 */
	public static void appendToText(String str, File destFile) throws Exception {
		destFile.getParentFile().mkdirs();
		FileWriter fw = new FileWriter(destFile, true);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(str);
		bw.write(PropertyConst.LINE_SEPARATOR);
		bw.flush();
		bw.close();
		fw.close();
	}
	
	public static boolean isExist(File srcFile, String s) throws Exception {
		boolean rv = false;
		if (!srcFile.exists()) {
			 throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
		}
		BufferedReader br = new BufferedReader(new FileReader(srcFile));
		String tempStr;
		while ((tempStr = br.readLine()) != null) {
			if (tempStr.indexOf(s) > -1) {
				rv = true;break;
			}
		}
		br.close();
		return rv;
	}
	
	/**************************************************************************
	 * @content: transfer xml data to object, return errorInfo
	 * @param  : abFilePath 文件路径
	 * @param  : k 属性
	 * @param  : v 属性值
	 * @author : 那建林
	 * @crtDate: 2012-07-20 09:48
	 * @modDate: 2012-07-20 09:48
	 */
	public synchronized static void changePropValue(String abFilePath, String k, String v) {
        Properties prop = new Properties(); 
        try { 
           InputStream inputStream = new FileInputStream(abFilePath); 
           //get the property list from the stream,it based thread... 
           prop.load(inputStream); 
           OutputStream fos = new FileOutputStream(abFilePath); 
           prop.setProperty(k, v); 
           //add or update the parameter,and add some comment... 
           prop.store(fos, "Last Modified");
           inputStream.close();
           fos.close();
        } catch (IOException e) { 
        	log.error("read file [" + abFilePath + "] error:" + e);
        } 
	} 
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		try {
			List<String> list = readByList(new File("d:/tmp/project.txt"), "GBK");
			for (String s : list) {
				s = s.replace("	", " ");
				String[] ary = s.split(" ");
				sb.append("INSERT INTO `project` VALUES ('"+ary[0]+"', '"+ary[1]+"', '"+ary[1]+"', 'user01', 'user02', 'active', NULL, '2017-5-11 15:58:45', '2017-5-11 15:58:45');");
				sb.append("\n");
			}
			overwriteToText(sb.toString(), new File("d:/tmp/projectAA.txt"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 转pdf文件
}

